'use strict';
// const Controller = require('egg').Controller;
const BaseController = require('./base');

class HotelController extends BaseController {
  // 查询所有的酒店信息
  async index() {
    const { ctx, service } = this;
    const { page, pageSize } = this.ctx.query;
    const { count } = await this.app.model.Hotel.findAndCountAll();
    if (page && pageSize) {
      const hotelInfo = await service.hotel.getHotelInfoList(page, pageSize);
      if (hotelInfo) {
        ctx.body = {
          code: 200,
          msg: '酒店获取成功',
          data: hotelInfo,
          pageInfo: {
            page, // 查询起始位置
            pageSize,

          },
          totalArticle: count,
        };

      }
    } else {
      // 不带分页查询时
      const getHotelList2 = await service.article.getArticle();
      if (getHotelList2) {
        this.ctx.body = {
          code: 200,
          msg: '酒店获取成功',
          data: getHotelList2,
          totalArticle: count,
        };
      }
      const hotelInfo = await service.hotel.getHotelInfoList();
      ctx.body = {
        code: 200,
        msg: 'ok',
        data: hotelInfo,
        total: count,
      };
    }
  }
  // 用过酒店的外键城市id进行查询信息
  async show() {
    try {
      const { ctx, service } = this;
      const id = ctx.params.id;
      const hotelInfo = await service.hotel.getHotelInfoListByForgienKeyId(id);
      ctx.body = {
        code: 200,
        msg: 'ok',
        data: hotelInfo,
      };
    } catch (error) {
      this.ctx.body = {
        code: 500,
        msg: '数据库查询错误',
      };
      return error;
    }
  }
  // 酒店id进行具体酒店查询
  async findByid() {
    try {
      const { ctx, service } = this;
      const id = ctx.params.id;
      const resultInfo = await service.hotel.getHotelInfoById(id);
      ctx.body = {
        code: 200,
        msg: 'ok',
        data: resultInfo,
      };
    } catch (error) {
      this.ctx.body = {
        code: 500,
        msg: '数据库查询错误',
      };
      return error;
    }
  }
  async create() {
    const { ctx, service } = this;
    const file = ctx.request.files[0];
    const data = ctx.request.body;
    const result = await service.uploadQiniu.upload(file);
    data.hotel_img = `http://reservation.mamenglei.top/${result.key}`;

    const addHotel = await service.hotel.addHotel(data);
    if (addHotel) {
      this.success(addHotel, '新增酒店成功');
    } else {
      this.error('新增酒店失败', 400);
    }
  }
  async update() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    const data = ctx.request.body;
    const updateHotel = await service.hotel.updateHotel(id, data);
    if (updateHotel) {
      this.success(updateHotel, '更新酒店信息成功');
    } else {
      this.error('更新酒店信息失败');
    }
  }
  async destroy() {
    const { ctx, service } = this;
    const hotel_id = ctx.params.id;
    console.log(hotel_id);
    const delHotel = await service.hotel.delHotel(hotel_id);
    if (delHotel) {
      this.success(delHotel, '删除酒店成功');
    } else {
      this.error('删除酒店失败');
    }
  }
  // 按照价格进行升序排序
  async sortPriceHotel() {
    const { service } = this;
    console.log('价格升序');
    const sortPrice = await service.hotel.sortPrice();
    this.success(sortPrice, '升序排列酒店');
  }
}
module.exports = HotelController;
